package com.lyn.seckill.controller;

import com.lyn.seckill.common.ResponseReturnType;
import com.lyn.seckill.common.error.BusinessException;
import com.lyn.seckill.common.error.EnumBusinessError;
import com.lyn.seckill.controller.viewovject.ItemVO;
import com.lyn.seckill.service.ItemService;
import com.lyn.seckill.service.model.ItemModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

@Controller
@CrossOrigin(allowedHeaders = "*",allowCredentials = "true")
public class ItemController extends BaseController{

    private final ItemService itemService;
    public ItemController(ItemService itemService) {
        this.itemService = itemService;
    }

    /**
     * 商品集合
     * @return
     */
    @GetMapping("/items")
    @ResponseBody
    public ResponseReturnType getItems() {
        List<ItemModel> itemModels = itemService.listItem();
        List<ItemVO> itemVOS = itemModels.stream().map(itemModel -> this.convertVOFromModel(itemModel)).collect(Collectors.toList());
        return ResponseReturnType.create(itemVOS);
    }

    /**
     * 商品详情
     * @param id
     * @return
     */
    @GetMapping("/items/{id}")
    @ResponseBody
    public ResponseReturnType getItem(@PathVariable("id")Integer id) {
        ItemModel itemById = itemService.getItemById(id);
        ItemVO itemVO = convertVOFromModel(itemById);
        return ResponseReturnType.create(itemVO);
    }

    /**
     * 创建商品
     * @param title
     * @param description
     * @param price
     * @param stock
     * @param imgUrl
     * @return
     * @throws BusinessException
     */
    @PostMapping(value = "/items",consumes = {CONTENT_TYPE_FORMED})
    @ResponseBody
    public ResponseReturnType createItem(@RequestParam("title")String title,
                                         @RequestParam("description")String description,
                                         @RequestParam("price")BigDecimal price,
                                         @RequestParam("stock")Integer stock,
                                         @RequestParam("imgUrl")String imgUrl) throws BusinessException {
        // 封装请求数据
        ItemModel itemModel = new ItemModel();
        itemModel.setTitle(title);
        itemModel.setDescription(description);
        itemModel.setPrice(price);
        itemModel.setStock(stock);
        itemModel.setImgUrl(imgUrl);
        // 请求服务创建商品
        ItemModel itemReturn = itemService.createItem(itemModel);
        ItemVO itemVO = this.convertVOFromModel(itemReturn);
        return ResponseReturnType.create(itemVO);
    }

    private ItemVO convertVOFromModel(ItemModel itemModel) {
        ItemVO itemVO = new ItemVO();
        BeanUtils.copyProperties(itemModel,itemVO);
        if (itemModel.getPromoModel() != null) {
            // 不为NULL，有正在进行或即将进行的活动
            itemVO.setPromoStatus(itemModel.getPromoModel().getStatus());
            itemVO.setPromoId(itemModel.getPromoModel().getId());
            itemVO.setStartTime(itemModel.getPromoModel().getStartTime());
            itemVO.setPromoPrice(itemModel.getPromoModel().getPromoItemPrice());
        } else {
            itemVO.setPromoStatus(EnumBusinessError.PROMO_NOT);
        }
        return itemVO;
    }
}
